/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;

import java.util.List;

/**
 * 学校管理Controller
 * @author yt
 * @version 2017-10-13
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfo/campusinfoSchool")
public class CampusinfoSchoolController extends BaseController {

	@Autowired
	private CampusinfoSchoolService campusinfoSchoolService;
	
	@ModelAttribute
	public CampusinfoSchool get(@RequestParam(required=false) String id) {
		CampusinfoSchool entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoSchoolService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoSchool();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfo:campusinfoSchool:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoSchool campusinfoSchool, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoSchool> page = campusinfoSchoolService.findPage(new Page<CampusinfoSchool>(request, response), campusinfoSchool);
		List<CampusinfoSchool> campusinfoSchools = campusinfoSchoolService.findList(campusinfoSchool);
		model.addAttribute("page", page);
		model.addAttribute("campusinfoSchools", campusinfoSchools);
		return "modules/campusinfo/campusinfoSchoolList";
	}

	@RequiresPermissions("campusinfo:campusinfoSchool:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoSchool campusinfoSchool, Model model) {
		model.addAttribute("campusinfoSchool", campusinfoSchool);
		return "modules/campusinfo/campusinfoSchoolForm";
	}

	@RequiresPermissions("campusinfo:campusinfoSchool:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoSchool campusinfoSchool, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoSchool)) {
			return form(campusinfoSchool, model);
		}
		// 获取请求对象中学校名称
		String name = campusinfoSchool.getSchoolname();
		// 根据请求对象中参数查询是否有数据
		CampusinfoSchool campusinfoSchoolname = campusinfoSchoolService.getBySchoolname(name);
		// 判断数据是否相同

        if(campusinfoSchoolname == null){
			campusinfoSchoolService.save(campusinfoSchool);
			addMessage(redirectAttributes, "保存学校信息成功");
		} else if (!StringUtils.isBlank(campusinfoSchool.getId())){
			campusinfoSchoolService.save(campusinfoSchool);
			addMessage(redirectAttributes, "修改学校信息成功");
		} else{
			if (name.equals(campusinfoSchoolname.getSchoolname())) {
				addMessage(redirectAttributes, "学校信息已经存在，请重新添加");
				//return "modules/campusinfo/campusinfoSchoolForm";
			} else {
				campusinfoSchoolService.save(campusinfoSchool);
				addMessage(redirectAttributes, "保存学校信息成功");
			}
		}

		return "redirect:" + Global.getAdminPath() + "/campusinfo/campusinfoSchool/?repage";
	}
	
	@RequiresPermissions("campusinfo:campusinfoSchool:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoSchool campusinfoSchool, RedirectAttributes redirectAttributes) {
		campusinfoSchoolService.delete(campusinfoSchool);
		addMessage(redirectAttributes, "删除学校信息成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfo/campusinfoSchool/?repage";
	}

}